package com.tangyh.basic.thirdpartutil;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tangyh.basic.alipay.bean.ElementIdentify;
import com.tangyh.basic.alipay.bean.IdentifyCommonParam;
import com.tangyh.basic.alipay.bean.IdentifyRequestParam;
import com.tangyh.basic.alipay.bean.PhotoRecognitionResult;
import com.tangyh.basic.base.AR;
import com.tangyh.lamp.common.enums.ElementIdentityType;
import com.tangyh.lamp.common.util.BeanTransUtil;
import com.tangyh.basic.utils.CommonUtil;
import com.tangyh.lamp.common.util.OkHttpUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * 第三方接口调用
 * <pre>
 *     1.身份证照片正反面认证
 *     2.二要素认证：姓名、身份证
 *     2.三要素认证：姓名、身份证、手机号
 *     2.银行卡四要素信息认证：银行卡、姓名、身份证、手机号
 * </pre>
 */
@Slf4j
public class ThirdPartApi {

    /**
     * 身份证图片识别接口
     * side (face：正面,back：背面)
     *
     * @return
     */
    public static AR<PhotoRecognitionResult> aliPhotoRecognition(IdentifyCommonParam commonParam, IdentifyRequestParam requestParam) {
        requestParam.setImage(CommonUtil.getImageBase64(requestParam.getImage()));
        AR<String> result = OkHttpUtils.httpPost(commonParam.getHost(), commonParam.getAuthorizationHeader(), JSONObject.toJSONString(requestParam));
        if (result.isSuccess()) {
            PhotoRecognitionResult recognitionResult = JSON.parseObject(result.getData(), PhotoRecognitionResult.class);
            return AR.success(recognitionResult);
        }
        System.out.println("Http code: " + result.getReturnCode());
        System.out.println("Http body error msg:" + result.getErrorMessage());
        log.error("Http code: [{}]", result.getReturnCode());
        log.error("Http body error msg: [{}]", result.getErrorMessage());
        return AR.fail(result.getReturnCode(), "身份证识别接口调用失败", result.getErrorMessage());
    }

    /**
     * 个人信息二要素检验接口:姓名、身份证<p>
     * 错误码定义:
     * <pre>
     * 01	实名认证通过！	实名认证通过！
     * 02	实名认证不通过！	实名认证不通过！
     * 202	无法验证！【中心中无此身份证记录，军人转业，户口迁移等】	无法验证！【中心中无此身份证记录，军人转业，户口迁移等】
     * 203	异常情况！	异常情况！
     * 204	姓名格式不正确！	姓名格式不正确！
     * 205	身份证格式不正确！	身份证格式不正确！
     * </pre>
     *
     * @param commonParam  请求公共参数
     * @param requestParam 请求验证参数
     * @return 查询结果
     */
    public static AR<ElementIdentify> twoElementsIdentify(IdentifyCommonParam commonParam, IdentifyRequestParam requestParam) {
        //状态码: 200 正常；400 URL无效；401 appCode错误； 403 次数用完； 500 API网管错误
        AR<String> result = OkHttpUtils.httpGet(commonParam.getHost(), commonParam.getAuthorizationHeader(), BeanUtil.beanToMap(requestParam));
        System.out.println(result.getData());
        if (result.isSuccess()) {
            ElementIdentify elementIdentify = JSON.parseObject(result.getData(), ElementIdentify.class);
            if (ElementIdentify.TWO_SUCCESS_CODE.equals(elementIdentify.getStatus())) {
                return AR.success(elementIdentify);
            }
            return AR.fail(String.valueOf(elementIdentify.getStatus()), elementIdentify.getMsg());
        }
        return AR.fail(result.getReturnCode(), "身份证识别接口调用失败", result.getErrorMessage());
    }


    /**
     * 个人信息三要素检验接口：姓名、身份证、手机号
     *
     * @param commonParam  请求公共参数
     * @param requestParam 请求验证参数
     * @return 查询结果
     */
    public static AR<ElementIdentify> threeElementsIdentify(IdentifyCommonParam commonParam, IdentifyRequestParam requestParam) {
        AR<String> result = OkHttpUtils.httpPost(commonParam.getHost(), commonParam.getAuthorizationHeader(), BeanTransUtil.beanToMapToLowerCase(requestParam));
        System.out.println(result.getData());
        if (result.isSuccess()) {
            ElementIdentify elementIdentify = JSON.parseObject(result.getData(), ElementIdentify.class);
            String res = elementIdentify.getResult().getRes();
            if (ElementIdentify.THREE_SUCCESS_CODE.equals(res)) {
                return AR.success(elementIdentify);
            }
            return AR.fail(String.valueOf(res), "认证结果：" + elementIdentify.getResult().getDescription());
        }
        JSONObject errorObject = CommonUtil.isJson(result.getErrorMessage());
        String error = "";
        if (errorObject != null) {
            error = CommonUtil.stringAppend(errorObject.get("message"), "(", errorObject.get("code"), ")");
        }
        return AR.fail(result.getReturnCode(), "身份证识别接口调用失败", error);
    }

    /**
     * 银行卡四要素信息认证：银行卡、姓名、身份证、手机号
     *
     * @param commonParam  请求公共参数
     * @param requestParam 请求验证参数
     * @return 查询结果
     */
    public static AR<ElementIdentify> fourElementsIdentifyOfBank(IdentifyCommonParam commonParam, IdentifyRequestParam requestParam) {
        AR<String> result = OkHttpUtils.httpGet(commonParam.getHost(), commonParam.getAuthorizationHeader(), BeanTransUtil.beanToMapToTransField(requestParam));
        System.out.println(result.getData());
        if (result.isSuccess()) {
            ElementIdentify elementIdentify = JSON.parseObject(result.getData(), ElementIdentify.class);
            String code = elementIdentify.getResp().getCode();
            if (ElementIdentify.FOUR_SUCCESS_CODE.equals(code)) {
                return AR.success(elementIdentify);
            }
            return AR.fail(String.valueOf(code), "认证结果：" + elementIdentify.getResp().getDesc());
        }
        return AR.fail(result.getReturnCode(), "身份证识别接口调用失败", result.getErrorMessage());
    }

    //测试
    public static void main(String[] args) {
        IdentifyCommonParam commonParam = new IdentifyCommonParam();
        commonParam.setHost("https://idcert.market.alicloudapi.com/idcard");
        commonParam.setAppcode("3bd295a088134907b9caa4c27d808e19");
        IdentifyRequestParam requestParam = new IdentifyRequestParam(ElementIdentityType.TWO_ELEMENTS_IDENTIFY, "刘松铭", "350702200208311834", null,
                null, null, null, null);
        AR<ElementIdentify> a = twoElementsIdentify(commonParam, requestParam);
        System.out.println("11111111");
    }
}
